package net.yla.smartbus.core.validation;

import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * @author KongXianDe
 * @date 2019/7/5 15:09
 */
public class CanNullLengthValidator implements ConstraintValidator<CanNullLength,Object> {

	/**
	 * 保存flag值
	 */
    private int min;

    private int max;

    @Override
    public void initialize(CanNullLength canNullLength) {
        // 注解内配的值赋值给变量
        this.min = canNullLength.min();
        this.max = canNullLength.max();
    }

    @Override
    public boolean isValid(Object obj, ConstraintValidatorContext constraintValidatorContext) {
        String value = String.valueOf(obj);
        if (StringUtils.isNotBlank(value) && (min > value.length() || value.length() > max)) {
            return false;
        }
        return true;
    }
}
